<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/09/09
 * Time: 11:47
 */
class ylyService
{

    private static $client_secret = '534abb55bc39a546b3e1328de6f9b012';//平台密钥 非空值;


    private static $request_url = 'https://open-api.10ss.net';
    /**
     * @param $data
     * @param $type
     * @return string
     */
    public function ServiceInlet($data,$type)
    {
        if(isset($data['issign']) === true){
            $data['sign'] = self::getSign($data);
        }
        if(isset($data['uuid']) === true){
            unset($data['uuid']);
            $data['id'] = self::Uuid4();
        }
        $data = http_build_query($data);
        $msg = '';
        switch($type){
            case 'acquiring':
                $url = self::$request_url.'/oauth/oauth';
                $msg = self::push($data,$url);
                break;

            case 'addPrint':
                $url = self::$request_url.'/printer/addprinter';
                $msg = self::push($data,$url);
                break;

            case 'delPrint':
                $url = self::$request_url.'/printer/deleteprinter';
                $msg = self::push($data,$url);
                break;

            case 'addMenu':
                $url = self::$request_url.'/printmenu/addprintmenu';
                $msg = self::push($data,$url);
                break;

            case 'Print':
                $url = self::$request_url.'/print/index';
                $msg = self::push($data,$url);
                break;

            case 'shutdownRestart':
                $url = self::$request_url.'/printer/shutdownRestart';
                $msg = self::push($data,$url);
                break;

            case 'setSound':
                $url = self::$request_url.'/printer/setsound';
                $msg = self::push($data,$url);
                break;

            case 'printInfo':
                $url = self::$request_url.'/printer/printinfo';
                $msg = self::push($data,$url);
                break;

            case 'cancelall':
                $url = self::$request_url.'/printer/cancelall';
                $msg = self::push($data,$url);
                break;

            case 'getversion':
                $url = self::$request_url.'/printer/getversion';
                $msg = self::push($data,$url);
                break;

            case 'cancelone':
                $url = self::$request_url.'/printer/cancelone';
                $msg = self::push($data,$url);
                break;

            case 'setlogo':
                $url = self::$request_url.'/printer/seticon';
                $msg = self::push($data,$url);
                break;

            case 'dellogo':
                $url = self::$request_url.'/printer/deleteicon';
                $msg = self::push($data,$url);
                break;

            case 'getorder':
                $url = self::$request_url.'/printer/getorder';
                $msg = self::push($data,$url);
                break;

            case 'scancodemodel':
                $url = self::$request_url.'/oauth/scancodemodel';
                $msg = self::push($data,$url);
                break;

            case 'btnprint':
                $url = self::$request_url.'/btn/btnprint';
                $msg = self::push($data,$url);
                break;

            default:
                $msg = '无效参数type';
        }
        return $msg;
    }


    /**
     * @param $data
     * @return string
     */
    private function getSign($data){
        $param = $data;
        unset($param['issign']);
        $sign = $param['client_id'].$param['timestamp'].self::$client_secret;
        return md5($sign);
    }

    /**
     * 获取唯一的uuid
     * @return string
     */
    private function Uuid4(){
        mt_srand((double)microtime() * 10000);
        $charid = strtolower(md5(uniqid(rand(), true)));
        $hyphen = '-';
        $uuidv4 =
            substr($charid, 0, 8) . $hyphen .
            substr($charid, 8, 4) . $hyphen .
            substr($charid, 12, 4) . $hyphen .
            substr($charid, 16, 4) . $hyphen .
            substr($charid, 20, 12);
        return $uuidv4;
    }



    /**
     * @param $data
     * @param $url
     * @return mixed
     */
    public static function push($data,$url)
    {
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检测
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Expect:'
        )); // 解决数据包大不能提交
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0); // 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包\
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        $tmpInfo = curl_exec($curl); // 执行操作
        if (curl_errno($curl)) {
            echo 'Errno' . curl_error($curl);
        }
        curl_close($curl); // 关键CURL会话
        return $tmpInfo; // 返回数据
    }

}